<?php

/**
 * orgao_externo actions.
 *
 * @package    camaras
 * @subpackage orgao_externo
 * @author     Gabriel Vaz
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class orgao_externoActions extends sfActions
{
  public function executeDesvincula(sfWebRequest $request)
  {

	$q = Doctrine_Query::create()
	  ->delete('UsrOrgExterno ue')
	  ->where('ue.cd_usuario = ?', $request->getParameter('cd_usuario'))
	  ->execute();
	  
	$this->redirect($this->getUser()->getAttribute('last_page',null,'usr'));
  }

  public function executeVincula(sfWebRequest $request)
  {
	$usr_org_externo = new UsrOrgExterno();
	$usr_org_externo->cd_usuario = $request->getParameter('cd_usuario');
	$usr_org_externo->cd_orgao_externo = $request->getParameter('cd_orgao_externo');
	$usr_org_externo->save();
	
	$this->redirect($this->getUser()->getAttribute('last_page',null,'usr'));
  }
  
  public function executeVincular(sfWebRequest $request)
  {
    $this->getUser()->setFlash('notice', 'Primeiro selecione o órgão externo!');			

	$this->redirect('orgao_externo/index');
  }
  
  public function executeIndex(sfWebRequest $request)
  {
    $this->orgao_externo_list = Doctrine::getTable('OrgaoExterno')
      ->createQuery('a')
  	  ->where('deleted = 0')
	  ->orderBy('name')
    ;
		  
    $this->pager = new sfDoctrinePager(
		'Usuario',
		sfConfig::get('app_qtd_registros_por_pagina')
	);
		
	$this->pager->setPage($request->getParameter('page', 1));
    $this->pager->setQuery($this->orgao_externo_list);
	$this->pager->init();
  }

  public function executeShow(sfWebRequest $request)
  {
	$this->getUser()->setAttribute('last_page', @$_SERVER['ORIG_PATH_INFO'], 'usr');
	$this->cd_orgao_externo = $request->getParameter('cd_orgao_externo');

	$this->usr_vinculado_list = Doctrine::getTable('UsrOrgExterno')->getUsrExtVinculados($request->getParameter('cd_orgao_externo'));
	$this->usr_nao_vinculado_list = Doctrine::getTable('Usuario')->getUsrExtNaoVinculados();

    $this->orgao_externo = Doctrine::getTable('OrgaoExterno')->find(array($request->getParameter('cd_orgao_externo')));
    $this->forward404Unless($this->orgao_externo);
  }

  public function executeNew(sfWebRequest $request)
  {
	if(!$this->getUser()->getAttribute('gerente',null,'usr')){
	  $this->getUser()->setFlash('error', sfConfig::get('app_msg_cadastro_exclusivo_gerente'));
	  $this->redirect('@homepage');
	} else {
      $this->form = new OrgaoExternoForm();
	}
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new OrgaoExternoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($orgao_externo = Doctrine::getTable('OrgaoExterno')->find(array($request->getParameter('cd_orgao_externo'))), sprintf('Object orgao_externo does not exist (%s).', array($request->getParameter('cd_orgao_externo'))));
    $this->form = new OrgaoExternoForm($orgao_externo);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($orgao_externo = Doctrine::getTable('OrgaoExterno')->find(array($request->getParameter('cd_orgao_externo'))), sprintf('Object orgao_externo does not exist (%s).', array($request->getParameter('cd_orgao_externo'))));
    $this->form = new OrgaoExternoForm($orgao_externo);

    $this->processForm($request, $this->form);

    $this->setTemplate('index');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($orgao_externo = Doctrine::getTable('OrgaoExterno')->find(array($request->getParameter('cd_orgao_externo'))), sprintf('Object orgao_externo does not exist (%s).', array($request->getParameter('cd_orgao_externo'))));
    $orgao_externo->delete();

    $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
    $this->redirect('orgao_externo/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $orgao_externo = $form->save();

      $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
      $this->redirect('orgao_externo/index');
    }
  }
}
